package org.kiwi.data.util;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * Created by jack on 16/10/29.
 */
public class DataSourceUtil {

    public static final String DEFAULT_VALIDATE_SQL = "SELECT 1 FROM dual";

    public static void doHeartBeat(DataSource dataSource) throws SQLException {
        Connection connection = null;
        PreparedStatement stmt = null;
        try {
            connection = dataSource.getConnection();
            stmt = connection.prepareStatement(DEFAULT_VALIDATE_SQL);
            stmt.executeQuery();
        } catch (SQLException e) {
            throw e;
        } finally {
            if (stmt != null)
                stmt.close();
            if (connection != null)
                connection.close();
        }
    }
}
